/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hello;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import javax.microedition.lcdui.List;
import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;
import javax.microedition.rms.RecordStoreNotOpenException;

/**
 *
 * @author Arthur
 */
final class ContatoDAO {
    private RecordStore rs = null;
    private List contatoLista; 
    private RecordEnumeration enume;
    private Comparator comp;
    
    public void openRMS() throws RecordStoreNotFoundException, RecordStoreFullException, 
            RecordStoreException{
        rs.openRecordStore("Catalogo de Enderecos", true);
    }
       
  
    public void incluirRMS(String nome, String fone, String cel, String email){
        
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        DataOutputStream dos = new DataOutputStream(baos);
        System.out.println(nome);
        System.out.println(fone);
        System.out.println(cel);
        System.out.println(email);
        try{
            dos.writeUTF(nome);
            dos.writeUTF(fone);
            dos.writeUTF(cel);
            dos.writeUTF(email);
            dos.flush();
            byte[] b = baos.toByteArray();
            int id = rs.addRecord(b, 0, b.length);
            
            baos.close();
            dos.close();
           // System.out.println(contato.getCelular());
        }catch(IOException e){
            e.printStackTrace();
       }
     catch (RecordStoreFullException e) {
         System.out.println("-- Não existe espaço disponível");
     }
     catch( RecordStoreNotOpenException e ){
         System.out.println("-- O Record Store esta fechado");
     }
     catch( RecordStoreException e ){
         System.out.println("-- Outro erro");
     }
    }
    
    public List findRMS(){
        
        try {
            enume = rs.enumerateRecords(null, comp, true);
            
        }catch(RecordStoreNotOpenException e){
            e.printStackTrace();
        }
                
        //add na lista
        ByteArrayInputStream bais = null;
        DataInputStream dis = null;
        byte[] b = null;
        while (enume.hasNextElement()){
            try{
                b= enume.nextRecord();
            }catch(RecordStoreException ex){
                ex.printStackTrace();
            }
            bais = new ByteArrayInputStream(b);
            dis = new DataInputStream(bais);
            try{
                contatoLista.append(dis.readUTF(), null);
                bais.close();
                dis.close();
            }catch(Exception e){
                e.printStackTrace();
            }
            
        }
        return contatoLista;
          
    }
    
    
    /*
    

 

             // Adiciona os dados convertendo-os para array de bytes

             rs.addRecord(baos.toByteArray(), 0, baos.toByteArray().length);

             showMessage ("Sucesso !");

       }
  }
     */
}